
package hotelroom;

//import statements
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Hendry Yuwono
 * 
 * This is the second panel
 * This will perform an "electronic key code" simulator
 * It will allow the user to key in the 6 digits password (2 variables and 4 numbers)
 */
public class InsertKey{
    /* creating object and variables */
    private JFrame f;
    private JPanel p;
    private JTextField jt;
    private JLabel label;
    private JButton j0;
    private JButton j1;
    private JButton j2;
    private JButton j3;
    private JButton j4;
    private JButton j5;
    private JButton j6;
    private JButton j7;
    private JButton j8;
    private JButton j9;
    private JButton jEnter;
    private JButton jClear;
    private int initial = 1;
    private int first;
    private int second;
    private int third;
    private int fourth;
    private String codeDB;
    private String code;
    private boolean stat;
    
    
    /**
     * this is a constructor
     */
    public void key(){
        //create a new frame
        f = new JFrame();
        f.setTitle("Inserting key...");
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //creating an object c
        GridBagConstraints c = new GridBagConstraints();
        //set default font
        Font  fo = new Font("Arial", Font.BOLD, 45);
        //create panel
        p = new JPanel(new GridBagLayout());
        p.setPreferredSize(new Dimension(400,500));
        p.setBackground(Color.LIGHT_GRAY);
        jt = new JTextField(6);
        jt.setEditable(false);
        
        //create label
        label = new JLabel("R - M -");
        label.setFont(fo);
        //create buttons
        j0 = new JButton("0");
        j1 = new JButton("1");
        j2 = new JButton("2");
        j3 = new JButton("3");
        j4 = new JButton("4");
        j5 = new JButton("5");
        j6 = new JButton("6");
        j7 = new JButton("7");
        j8 = new JButton("8");
        j9 = new JButton("9");
        jEnter = new JButton("Enter");
        jEnter.setBackground(Color.GREEN);
        jClear = new JButton("Clear");
        jClear.setBackground(Color.YELLOW);
        
        //add action listener on every button
        j0.addActionListener(new Handler());
        j1.addActionListener(new Handler());
        j2.addActionListener(new Handler());
        j3.addActionListener(new Handler());
        j4.addActionListener(new Handler());
        j5.addActionListener(new Handler());
        j6.addActionListener(new Handler());
        j7.addActionListener(new Handler());
        j8.addActionListener(new Handler());
        j9.addActionListener(new Handler());
        jEnter.addActionListener(new Handler());
        jClear.addActionListener(new Handler());
        
        //set textfield string format
        jt.setText("R"+" - "+"M"+" - "+first+" - "+second+" - "+third+" - "+fourth);
        jt.setFont(fo);
        
        
        //allocate the attributes
        c.weightx=0.5;
        c.weighty=0.5;
        
        
        c.ipady = 20;
        c.ipadx = 40;
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.CENTER;
        c.insets = new Insets(0,0,0,0);
        c.gridx = 1;
        c.gridy = 6;
        p.add(j0, c);
        c.gridx = 0;
        c.gridy = 5;
        p.add(j1, c);
        c.gridx = 1;
        c.gridy = 5;
        p.add(j2, c);
        c.gridx = 2;
        c.gridy = 5;
        p.add(j3, c);
        c.gridx = 0;
        c.gridy = 4;
        p.add(j4, c);
        c.gridx = 1;
        c.gridy = 4;
        p.add(j5, c);
        c.gridx = 2;
        c.gridy = 4;
        p.add(j6, c);
        c.gridx = 0;
        c.gridy = 3;
        p.add(j7, c);
        c.gridx = 1;
        c.gridy = 3;
        p.add(j8, c);
        c.gridx = 2;
        c.gridy = 3;
        p.add(j9, c);
        
        c.ipady = 25;
        c.ipadx = 60;
        c.gridx = 2;
        c.gridy = 6;
        p.add(jEnter, c);
        c.gridx = 0;
        c.gridy = 6;
        p.add(jClear, c);
        
        c.fill = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 0;
        c.ipady = 20;
        c.ipadx = 500;
        c.gridwidth = 3;
        c.insets = new Insets(5,5,2,2);  
        p.add(jt, c);
        
        //add panel to the frame
        f.getContentPane().add(p, BorderLayout.CENTER);
        f.pack();
    }
    
    //this class implements action listener that give some instructions to every button 
    class Handler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
            
            //button number 0
            if (e.getSource() == j0){
                if(initial == 1){
                    setNum(0,initial);
                }else if(initial == 2){
                    setNum(0,initial);
                }else if(initial == 3){
                    setNum(0,initial);
                }else if(initial == 4){
                    setNum(0,initial);
                }
                ++initial;
                
                //button number 1
            }else if(e.getSource() == j1){
                if(initial == 1){
                    setNum(1,initial);
                    
                }else if(initial == 2){
                    setNum(1,initial);
                }else if(initial == 3){
                    setNum(1,initial);
                }else if(initial == 4){
                    setNum(1,initial);
                }
                ++initial;
                
                //button number 2
            }else if(e.getSource() == j2){
                if(initial == 1){
                    setNum(2,initial);
                    
                }else if(initial == 2){
                    setNum(2,initial);
                }else if(initial == 3){
                    setNum(2,initial);
                }else if(initial == 4){
                    setNum(2,initial);
                }
                ++initial;
                
                //button number 3
            }else if(e.getSource() == j3){
                if(initial == 1){
                    setNum(3,initial);
                    
                }else if(initial == 2){
                    setNum(3,initial);
                }else if(initial == 3){
                    setNum(3,initial);
                }else if(initial == 4){
                    setNum(3,initial);
                }
                ++initial;
                
                //button number 4
            }else if(e.getSource() == j4){
                if(initial == 1){
                    setNum(4,initial);
                    
                }else if(initial == 2){
                    setNum(4,initial);
                }else if(initial == 3){
                    setNum(4,initial);
                }else if(initial == 4){
                    setNum(4,initial);
                }
                ++initial;
                
                //button number 5
            }else if(e.getSource() == j5){
                if(initial == 1){
                    setNum(5,initial);
                    
                }else if(initial == 2){
                    setNum(5,initial);
                }else if(initial == 3){
                    setNum(5,initial);
                }else if(initial == 4){
                    setNum(5,initial);
                }
                ++initial;
                
                //button number 6
            }else if(e.getSource() == j6){
                if(initial == 1){
                    setNum(6,initial);
                    
                }else if(initial == 2){
                    setNum(6,initial);
                }else if(initial == 3){
                    setNum(6,initial);
                }else if(initial == 4){
                    setNum(6,initial);
                }
                ++initial;
                
                //button number 7
            }else if(e.getSource() == j7){
                if(initial == 1){
                    setNum(7,initial);
                    
                }else if(initial == 2){
                    setNum(7,initial);
                }else if(initial == 3){
                    setNum(7,initial);
                }else if(initial == 4){
                    setNum(7,initial);
                }
                ++initial;
                
                //button number 8
            }else if(e.getSource() == j8){
                if(initial == 1){
                    setNum(8,initial);
                    
                }else if(initial == 2){
                    setNum(8,initial);
                }else if(initial == 3){
                    setNum(8,initial);
                }else if(initial == 4){
                    setNum(8,initial);
                }
                ++initial;
                
                //button number 9
            }else if(e.getSource() == j9){
                if(initial == 1){
                    setNum(9,initial);
                }else if(initial == 2){
                    setNum(9,initial);
                }else if(initial == 3){
                    setNum(9,initial);
                }else if(initial == 4){
                    setNum(9,initial);
                }
                ++initial;
            }else if(e.getSource() == jEnter){
                if ((first == 0 )&&(second == 0)&&(third == 0)&&(fourth == 0)){
                    //default message
                    JOptionPane.showMessageDialog(null, String.format("%s", "Please input 4-digits code"));
                }else{
                    //convert integer to string
                    convertToString(first,second,third,fourth);
                    
                    if (code.equals(getCodeDB())){
                        //success message
                        JOptionPane.showMessageDialog(null, String.format("%s", "Your code: " + code));
                        //dispose the frame
                        f.dispose();
                        setStatus(true);
                    }else{
                        //error message
                        JOptionPane.showMessageDialog(null, String.format("%s", "Error code: " + code));
                    }
                    //set to default
                    clear();
                }
            }else if(e.getSource() == jClear){
                //set to default
                clear();
            }
            jt.setText("R"+" - "+"M"+" - "+first+" - "+second+" - "+third+" - "+fourth);
            f.repaint();
        }
    }
    
    //method clear() to set the contents on panel to the default
    public void clear(){
        first=0;
        second=0;
        third=0;
        fourth=0;
        initial = 1;
        f.repaint();
    }
    
    /**
     * 
     * @param s will take boolean value
     */
    public void setStatus(boolean s){
        stat = s;
    }
    /**
     * 
     * @return return boolean value 
     */
    public boolean getStatus(){
        return stat;
    }
    
    /**This method works as a converter from integer to string format
     * 
     * @param a will take int value
     * @param b will take int value
     * @param c will take int value
     * @param d will take int value
     */ 
    public void convertToString(int a, int b, int c, int d){
            String d1 = new Integer(a).toString();
            String d2 = new Integer(b).toString();
            String d3 = new Integer(c).toString();
            String d4 = new Integer(d).toString();
            
            code = "R"+"M"+d1+d2+d3+d4;
        }
        /**
         * 
         * @return return object 'code' that contains string
         */
        public String getCode(){
            return code;
        }
        
        /**
         * 
         * @param n will take int value
         * @param i will take int value
         */
        public void setNum(int n, int i){
            if (i == 1){
                first = n;
            }else if(i == 2){
                second = n;
            }else if(i == 3){
                third = n;
            }else if(i == 4){
                fourth = n;
            }
        }
        
        /**
         * 
         * @return return integer
         */
        public int getNum(){
            if (initial == 1){
                return first;
            }else if(initial == 2){
                return second;
            }else if(initial == 3){
                return third;
            }else if(initial == 4){
                return fourth;
            }return 0;
        }
        
        /**
         * 
         * @param i will take string value
         */
        public void setCodeDB(String i){
            codeDB = i;
        }
        
        /**
         * 
         * @return return string
         */
        public String getCodeDB(){
            return codeDB;
        }
}
